var co = require('/resource/js/common.js')
var session_key
var _this

App({
   Data: {
      domain: 'http://o2o.igoyp.com',
      app: {
         appid: 'wx66040e1a16ab65d4',
         secret: '7c19138e858420ef1125f2d7752e5e83'
      },
      reg_from: 'repair_wxapp',
      p: {
         latitude: 26.063939,//26.100216398141626,
         longitude: 119.350609,//119.29580735449221,
      },
      userInfo: '',
      shopInfo: '',
      tableCateArr: '',
      loginMode: 'guest',
      guestInfo: '',
      wssIsInit: 0,
      isWsSender: 0,//websocket发送者，1是，0否
      client_id: '',
      rid: '',//授权登录room_id
      //本店状态
      takeOut: 0,
      // 选择桌信息
      Table_ID: '',
      tableInfo: '',
      Order_ID: '',
      // 添加菜单返回信息
      tableOrder: ''
   },
   // 进入页面加载
   onLaunch: function () {
      _this = this
      this.loginStatic()
   },
   // 获取商家信息
   getShopInfo(biz, callback) {
      var param = {
         Biz_ID: biz,
         Skin_ID: 2
      }
      co.getShareApi('biz/Bmg/getInfo', param, function (res) {
         if (res.code == 0) {
            if (typeof (callback) == 'function') {
               callback(res)
            }
            _this.Data.shopInfo = res.data
         }
      })
   },
   //登录状态
   loginStatic(session_key) {
      if (!session_key) {
         this.wxLogin()
      } else {
         this.getWXUserInfo()
      }
   },
   //微信登录授权 
   wxLogin() {
      wx.login({
         success: function (res) {
            _this.getWxSession(res.code)
         },
         fail: function (res) { },
      })
   },
   //获取微信session_key保存起来，长时间有效可用
   getWxSession(code) {
      var param = this.Data.app
      param.code = code
      co.getShareApi('api/Weixin/getSessionKey', param, function (res) {
         wx.setStorageSync('unionid', res.data.unionid)
         wx.setStorageSync('session_key', res.data.session_key)
         _this.getWXUserInfo()
      })
   },
   //获取用户数据方法
   getWXUserInfo(callBack) {
      wx.getUserInfo({
         withCredentials: true,
         lang: '',
         success: function (res) {
            // console.log('【userInfo:res】', res)
            // if (typeof (callBack) == 'function') {
            //     callBack(res.userInfo)
            // }
            _this.decryptWxUserInfoData(res, callBack)
         },
      })
   },
   //解密和获取微信用户信息
   decryptWxUserInfoData(res, callBack) {
      var param = {
         appid: this.Data.app.appid,
         session_key: wx.getStorageSync('session_key'),
         iv: res.iv,
         encryptedData: res.encryptedData
      }
      if (wx.getStorageSync('Owner_Id')) {
         param.Owner_Id = wx.getStorageSync('Owner_Id')
         // console.log('【获取用户信息带上了要请人ID】==' + param.invite_id)
      }
      co.getShareApi('user/Mg/getInfo', param, function (res) {
         if (res.code == 0) {
            _this.Data.userInfo = res.data
            // 保存用户信息
            wx.setStorageSync('userInfo', res.data)
            console.log("【用户信息app】===", res.data)
            if (typeof (callBack) == 'function') {
               callBack(res.data)
            }
         } else if (res.code == 93748) {
            _this.wxLogin()   //解密失败可能是session_key失效了，需重新登录获取session_key
         }
      })
   },
   //获取分享信息
   getShareInfo(share_res, pageName, pagePath) {
      var shareTickets = share_res.shareTickets;
      if (shareTickets.length == 0) {
         return false;
      }
      wx.getShareInfo({
         shareTicket: shareTickets[0],
         success: function (res) {
            console.log("【获取分享信息wx.getShareInfo】===", res)
            _this.decryptShareInfo(res, pageName, pagePath)
         }
      })
   },
   //解密群ID
   decryptShareInfo(data, pageName, pagePath) {
      var param = {
         appid: this.Data.app.appid,
         session_key: wx.getStorageSync('session_key'),
         iv: data.iv,
         encryptedData: data.encryptedData
      }
      co.getShareApi('api/Weixin/decryptInfo', param, function (res) {
         if (res.code == 0) {
            //得到群ID
            console.log("【获取群ID】===", res)
            // 显示群名称wxml组件代码：<open-data type="groupName" open-gid="{{openGId}}" />
            // _this.setData({ openGId: res.data.openGId })
            _this.forwardShare(res.data.openGId, pageName, pagePath)
         }
      })
   },
   //保存转发分享
   forwardShare: function (gid, pageName, path) {
      var param = {
         User_ID: this.Data.userInfo.User_ID,
         Share_Page: pageName,
         Wx_Group_ID: gid,
         Share_Page_Path: path
      }
      co.getShareApi('share/Shmg/forwardShare', param, function (res) {
         console.log("【转发分享】", res.data)
      })
   },
   setHomePageShowAvatarUrlNotice: function (callback) {
      this.homePageCallback = callback
   },

   noticeHomePageShowAvatarUrl: function (res) {
      if (typeof (this.homePageCallback) == 'function') {
         this.homePageCallback(res)
      }
   },


   /* 重设按钮号码：
    * NoArray：按钮号码[二维数组]，0为[更多]按钮，-1为[返回]按钮；
    * No：点中的按钮值；
    * num：初始按钮正数号码的个数
    */
   reSetNum: function (NoArray, No, num = 15) {
      var nA = numArray    //this.data.numArray
      var num2 = num - 1
      var sum = num + num2
      for (var i = 0; i < nA.length; i++) {
         var nA2 = nA[i]
         for (var j = 0; j < nA2.length; j++) {
            if (No == 0) {
               if (nA[i][j] == num) nA[i][j] = -1
               else if (nA[i][j] > num) nA[i][j] += num2
               else if (nA[i][j] > 0) nA[i][j] += num
            } else {
               if (nA[i][j - 1] == num2) nA[i][j] = num
               else if (nA[i][j] > sum) nA[i][j] -= num2
               else if (nA[i][j] > 0) nA[i][j] -= num
            }
         }
      }
      return nA
      //  this.setData({ numArray: nA })
   },
})
